package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class MyHousePresenter extends BasePresenter<MineContract.Model, MineContract.HouseView> {

    public static final int ADD = 1;
    public static final int DEL = 2;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public MyHousePresenter(MineContract.Model model, MineContract.HouseView view) {
        super(model, view);
    }

    public void fetchHouseList() {
        mModel.fetchMyHouse()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<MyHouseResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<MyHouseResult> result) {
                        mRootView.onFetchHouse(result);
                    }
                });
    }

    public void fetchHouseMember(int roomId) {
        mModel.fetchHouseMember(roomId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<MyHouseMemberResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<MyHouseMemberResult> result) {
                        mRootView.onFetchHouseMember(result);
                    }
                });
    }

    public void addMember(String name, String phone, String idCard, int type, int roomId) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("phone", phone);
        params.put("type", type);
        params.put("idCardNo", idCard);
        params.put("roomId", roomId);

        mModel.addHouseMember(params)
                .doOnSubscribe(act -> mRootView.showLoading("正在提交..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ADD) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void delMember(int ownerRoomId) {
        mModel.delHouseMember(ownerRoomId)
                .doOnSubscribe(act -> mRootView.showLoading("正咋删除..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, DEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void fetchDevice() {
        mModel.fetchDoorAccessResult()
                .doOnSubscribe(act -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<DoorAccessDeviceResult>() {
                    @Override
                    public void onSuccess(@NonNull DoorAccessDeviceResult result) {
                        mRootView.onFetchDoorAccess(result);
                    }
                });
    }

    public void fetchDeviceEventRecords(String month, int page, int pageSize) {
        mModel.fetchDoorRecord(month, page, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<DoorAccessRecordResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<DoorAccessRecordResult> result) {
                        mRootView.onFetchDoorRecord(result.getList());
                    }
                });
    }

}
